﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.DAL;
using WXYW.Entities;
using WXYW.Model;

namespace WXYW.BLL
{
    public class UserService : RepositoryBase
    {
        public User Logon(string userName, string password)
        {
            User userEntity = GetEntities<User>(u => u.UserName.Equals(userName) && u.UserPassword.Equals(password)).FirstOrDefault();
            if (userEntity != null && !userEntity.UserTypeReference.IsLoaded)
            {
                userEntity.UserTypeReference.Load();
            }
            return userEntity;
        }

        public User GetUserByID(long userID)
        {
            User userEntity = GetEntities<User>(u => u.UserID == userID).FirstOrDefault();
            if (userEntity != null && !userEntity.UserTypeReference.IsLoaded)
            {
                userEntity.UserTypeReference.Load();
            }
            return userEntity;
        }

        public User GetUserByName(string userName)
        {
            User userEntity = GetEntities<User>(u => u.UserName.Equals(userName)).FirstOrDefault();
            if (userEntity != null && !userEntity.UserTypeReference.IsLoaded)
            {
                userEntity.UserTypeReference.Load();
            }
            if (userEntity != null && userEntity.ServiceStationReference.IsLoaded)
            {
                userEntity.ServiceStationReference.Load();
            }
            return userEntity;
        }

        public IQueryable<User> GetUserConnectPersonByPrefixText(string name, int count)
        {
            return GetEntities<User>(u => u.ContactPerson.Contains(name) && u.UserType.ID == (int)RoleType.Customer).Take(count);
        }

        public IQueryable<User> GetUserCompanyByprefixText(string companyName, int count)
        {
            return GetEntities<User>(u => u.FullName.Contains(companyName) && u.UserType.ID == (int)RoleType.Customer).Take(count);
        }

        public List<User> GetUsersByType(int type)
        {
            return GetEntities<User>(u => u.UserType.ID == type).ToList();
        }

        public List<User> GetAllEngineer()
        {
            return GetEntities<User>(u => u.UserType.ID == (int)RoleType.Engineer || u.UserType.ID == (int)RoleType.PlatformEngineer).ToList();
        }

        public User GetUserByApplicationFormID(long applicationFormID)
        {
            ApplicationForm form = GetEntities<ApplicationForm>(p => p.ApplicationID == applicationFormID).FirstOrDefault();
            if (!form.UserReference.IsLoaded)
            {
                form.UserReference.Load();
            }
            if (!form.User.UserTypeReference.IsLoaded)
            {
                form.User.UserTypeReference.Load();
            }
            return form.User;
        }

        public int GetServiceStationIDByUserID(long userID)
        {
            User user = GetEntities<User>(u => u.UserID == userID).FirstOrDefault();
            if (!user.ServiceStationReference.IsLoaded)
            {
                user.ServiceStationReference.Load();
            }

            return user.ServiceStation.ID;
        }

        public void AddUser(string userID, string contact, string phone, string mobilePhone, string fax, string companyName,
            string password, int roleType, string email, string fullName, string address, string comment, long updatedBy,
            int stationID, int OEMClassID)
        {
            User newUser = new User();
            newUser.Comment = comment;
            newUser.CompanyName = companyName;
            newUser.ContactPerson = contact;
            newUser.FullName = fullName;
            newUser.LastUpdated = DateTime.Now;
            newUser.UserAddress = address;
            newUser.UserName = userID;
            newUser.UserPassword = password;
            newUser.UserPhone = phone;
            newUser.UserMobilePhone = mobilePhone;
            newUser.UserType = GetEntities<UserType>().Where(t => t.ID == roleType).FirstOrDefault();
            newUser.UpdatedBy = updatedBy;
            newUser.UserFax = fax;
            newUser.UserEmail = email;
            if (stationID != 0)
            {
                newUser.ServiceStation = GetEntities<ServiceStation>().Where(s => s.ID == stationID).FirstOrDefault();
            }
            if (OEMClassID != 0)
            {
                newUser.ProductClass = GetEntities<ProductClass>().Where(c => c.ID == OEMClassID).FirstOrDefault();
            }
            InsertEntity(newUser);
        }

        public void UpdateUser(long userID, string userName, string fullName, string contact, string phone, string mobilePhone,
            string email, string address, string company, string comment, long updatedBy)
        {
            User updateUser = GetEntities<User>().Where(u => u.UserID == userID).FirstOrDefault();
            updateUser.UserName = userName;
            updateUser.FullName = fullName;
            updateUser.ContactPerson = contact;
            updateUser.UserPhone = phone;
            updateUser.UserMobilePhone = mobilePhone;
            updateUser.CompanyName = company;
            updateUser.Comment = comment;
            updateUser.UserEmail = email;
            updateUser.UserAddress = address;
            UpdateEntity(updateUser);
        }

        public void UpdateUser(User user)
        {
            UpdateEntity(user);
        }

        public void DeleteUser(long userID)
        {
            DeleteEntity<User>(u => u.UserID == userID, Guid.Empty);
        }

        public List<User> GetUsersByKeyword(string keyword, int type)
        {
            List<User> userList = new List<User>();
            if (string.IsNullOrEmpty(keyword))
            {
                userList = GetEntities<User>(u => u.UserType.ID == type).ToList();
            }
            else
            {
                userList = GetEntities<User>(u => (u.UserName.Contains(keyword) || u.FullName.Contains(keyword) || u.ContactPerson.Contains(keyword)
                || u.UserPhone.Contains(keyword) || u.UserEmail.Contains(keyword)) && u.UserType.ID == type).ToList();
            }

            return userList;
        }

        public List<User> GetUsersByKeyword(string keyword)
        {
            List<User> userList = new List<User>();
            if (string.IsNullOrEmpty(keyword))
            {
                userList = GetEntities<User>().ToList();
            }
            else
            {
                userList = GetEntities<User>(u => (u.UserName.Contains(keyword) || u.FullName.Contains(keyword) || u.ContactPerson.Contains(keyword)
                || u.UserPhone.Contains(keyword) || u.UserEmail.Contains(keyword))).ToList();
            }

            foreach (User user in userList)
            {
                if (!user.UserTypeReference.IsLoaded)
                {
                    user.UserTypeReference.Load();
                }
            }
            return userList;
        }

        public List<SimpleUserModel> GetUserModelsByKeywork(string keyword)
        {
            List<SimpleUserModel> usersModel = new List<SimpleUserModel>();
            List<User> userList = new List<User>();
            if (string.IsNullOrEmpty(keyword))
            {
                userList = GetEntities<User>().ToList();
            }
            else
            {
                userList = GetEntities<User>(u => (u.UserName.Contains(keyword) || u.FullName.Contains(keyword) || u.ContactPerson.Contains(keyword)
                || u.UserPhone.Contains(keyword) || u.UserEmail.Contains(keyword))).ToList();
            }
            foreach (User userEntity in userList)
            {
                SimpleUserModel userModle = new SimpleUserModel();
                userModle.Address = userEntity.UserAddress;
                userModle.ContactName = userEntity.ContactPerson;
                userModle.Email = userEntity.UserEmail;
                userModle.FullName = userEntity.FullName;
                userModle.ID = userEntity.UserID.ToString();
                userModle.UserID = userEntity.UserName;
                if (!userEntity.UserTypeReference.IsLoaded)
                {
                    userEntity.UserTypeReference.Load();
                }
                userModle.UserType = userEntity.UserType.Name;
                userModle.UserPhone = userEntity.UserPhone;
                userModle.MobilePhone = userEntity.UserMobilePhone;
                if (!userEntity.ServiceStationReference.IsLoaded)
                {
                    userEntity.ServiceStationReference.Load();
                }
                if (userEntity.ServiceStation != null)
                {
                    userModle.Station = userEntity.ServiceStation.Name;
                }
                usersModel.Add(userModle);
            }
            return usersModel;
        }

        public List<SimpleUserModel> GetAllUserList(string keyword)
        {
            List<SimpleUserModel> usersModel = new List<SimpleUserModel>();
            IQueryable<User> usersEntity = GetEntities<User>();
            if (!string.IsNullOrEmpty(keyword))
            {
                usersEntity = usersEntity.Where(u => u.UserAddress.Contains(keyword) || u.ContactPerson.Contains(keyword) || u.UserEmail.Contains(keyword)
                    || u.FullName.Contains(keyword) || u.UserType.Name.Contains(keyword) || u.ServiceStation.Name.Contains(keyword));
            }
            foreach (User userEntity in usersEntity)
            {
                SimpleUserModel userModle = new SimpleUserModel();
                userModle.Address = userEntity.UserAddress;
                userModle.ContactName = userEntity.ContactPerson;
                userModle.Email = userEntity.UserEmail;
                userModle.FullName = userEntity.FullName;
                userModle.ID = userEntity.UserID.ToString();
                userModle.UserID = userEntity.UserName;
                if (!userEntity.UserTypeReference.IsLoaded)
                {
                    userEntity.UserTypeReference.Load();
                }
                userModle.UserType = userEntity.UserType.Name;
                userModle.UserPhone = userEntity.UserPhone;
                userModle.MobilePhone = userEntity.UserMobilePhone;
                if (!userEntity.ServiceStationReference.IsLoaded)
                {
                    userEntity.ServiceStationReference.Load();
                }
                if (userEntity.ServiceStation != null)
                {
                    userModle.Station = userEntity.ServiceStation.Name;
                }
                usersModel.Add(userModle);
            }
            return usersModel;
        }

        public IQueryable<UserType> GetUserTypeList()
        {
            return GetEntities<UserType>();
        }

        public List<User> GetEngineers()
        {
            List<User> engineers = new List<User>();
            List<User> userEntities = GetEntities<User>().Where(u => u.UserType.ID == (int)RoleType.Engineer || u.UserType.ID == (int)RoleType.PlatformEngineer).ToList();
            engineers.Add(new User { UserID = 0, UserName = "请选择" });
            engineers.AddRange(userEntities);
            return engineers;
        }

        public List<User> GetUsersByRoleType(int type)
        {
            return GetEntities<User>(u => u.UserType.ID == type).ToList();
        }

        public List<User> GetEngineers(int serviceStationID)
        {
            List<User> engineers = new List<User>();
            List<User> userEntities = GetEntities<User>().Where(u => (u.UserType.ID == (int)RoleType.Engineer || u.UserType.ID == (int)RoleType.PlatformEngineer) && u.ServiceStation.ID == serviceStationID).ToList();
            engineers.Add(new User { UserID = 0, UserName = "请选择", ContactPerson = "请选择" });
            engineers.AddRange(userEntities);
            return engineers;
        }

        public List<User> GetSerciceStationEngineer(int serviceStationID)
        {
            return GetEntities<User>().Where(u => (u.UserType.ID == (int)RoleType.Engineer || u.UserType.ID == (int)RoleType.PlatformEngineer) && u.ServiceStation.ID == serviceStationID).ToList();
        }

        public List<User> GetUserOfStation(int stationID)
        {
            List<User> results = new List<User>();
            results.Add(new User
            {
                UserID = 0,
                UserName = "全部"
            });

            IQueryable<User> users = GetEntities<User>().Where(u => u.ServiceStation.ID == stationID).OrderBy(u => u.UserName);
            results.AddRange(users);
            return results;
        }

        public object GetWorkingPeople()
        {
            return GetEntities<User>(u => u.UserType.ID != (int)RoleType.Customer).OrderBy(u=>u.ContactPerson);
        }

        public int GetPermission(int menuID, int roleID)
        {
            int permission = 0;
            //if (menuID == 0 || (roleID != 2 && roleID != 4 && roleID != 5 && roleID != 6 && roleID != 16 &&
            //    roleID != 20 && roleID != 9))
            //{
            //    permission = 1;
            //}
            //else
            //{
            RoleMenu roleMenu = GetEntities<RoleMenu>().Where(r => r.Menu.ID == menuID && r.UserType.ID == roleID).FirstOrDefault();
            if (roleMenu != null)
            {
                permission = roleMenu.Permission;
            }
            //}
            return permission;
        }

        public int HasPermissionOfSubMenu(int parentMenuID, int roleID)
        {
            IQueryable<RoleMenu> roleMenus = GetEntities<RoleMenu>().Where(r => r.Menu.ParentID == parentMenuID && r.Permission > 0 && r.UserType.ID == roleID);
            if (roleMenus.Count() > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public UserType GetUserTypeByID(int typeID)
        {
            return GetEntities<UserType>().Where(u => u.ID == typeID).FirstOrDefault();
        }

        public User GetUserByContactPerson(string name)
        {
            return GetEntities<User>(u => u.ContactPerson == name && u.UserType.ID == (int)RoleType.Customer).FirstOrDefault();
        }

        public User GetUserByCompanyName(string companyName)
        {
            return GetEntities<User>(u => u.FullName == companyName && u.UserType.ID == (int)RoleType.Customer).FirstOrDefault();
        }

        internal List<User> GetAdminAndQuatationer()
        {
            // 哪个type是报价专员
            return GetEntities<User>(u => u.UserType.ID == (int)RoleType.Administrator).ToList();
        }

        public List<User> GetUserByProductClassID(int productClassID)
        {
            return GetEntities<User>().Where(p => p.ProductClass.ID == productClassID).ToList();
        }
    }
}
